#coding=utf-8
# python的默认是用ascii。如果没有上面这句，中文不能通过

import math
import os
import string
import random

# 查询letter在word中出现的次数
# 反转词典,键值交换
def invert_dict(d):
    inverse = dict()
    for key in d:
        val = d[key]
        if val not in inverse:
            inverse[val] = [key]
        else:
            inverse[val].append(key)
    return inverse

print invert_dict(dic)
input()

count = 0
def ex():
    global count
    count = count + 1
    print count

# ex()

def sumall(*para):
    s=0
    for ndx in para:
        s += ndx
    return s

# print sumall(1,2,3)

# concentrate two sequence
d = dict(zip('abc',range(3)))  
# print d

def sort_by_length(words):
    t=[]
    for word in words:
        t.append((len(word),word))

    t.sort(reverse=True) # sort descending

    res = []
    for length,word in t:
        res.append(word)
    return res

# print sort_by_length(['afds','dfsffs','a'])


def randomtest():
    for i in range(10):
        x = random.random()
        print x
    print random.randint(2,9)
    t=[1,2,3]
    print random.choice(t)

# randomtest()

# get word histogram
def process_file(filename):
    dic = dict()
    fp = open(filename)
    for line in fp:
        process_line(line,dic)
    return dic

def process_line(line,hist):
    line = line.replace('-',' ')
    for word in line.split():
        word.strip(string.punctuation + string.whitespace)
        word.lower()

        hist[word] = hist.get(word,0) + 1

# print process_file('tmp.txt')

def walk(dirname):
    for name in os.listdir(dirname):
        path = os.path.join(dirname,name)

        if os.path.isfile(path):
            print path
        else:
            walk(path)

# walk('.')
# raw_input()

class Time:
    def __init__(self,h=0,m=0,s=0):
        self.hour = h
        self.minute = m
        self.second = s
    def print_time(self):
        print '%.2d:%.2d:%.2d' %(self.hour,self.minute,self.second)

# t=Time()
# t.print_time()

def renameFiles(path,prefix):
    files = os.listdir(path)
    index = 1
    for file in files:
        filename = os.path.splitext(file)[0][:]
        suffix = os.path.splitext(file)[1]

        srcpath = os.path.join(path,file)
        destpath = path + '\\' + prefix + '_%04d'%(index) + suffix
        os.rename(srcpath,destpath)

        index += 1

path = u'h:\\pic'

print path

prefix = 'IMG_2014'

renameFiles(path,prefix)
raw_input("press Enter to exit")
sys.exit()



'''
# some function exercise

def countdown(n):
    if n <= 0:
        print 'end'
        return
    else:
        print n
        countdown(n-1)

def distance(x1,y1,x2,y2):
    dx = x2-x1
    dy = y2-y1
    return math.sqrt(dx**2 + dy**2)

def check(n):
    if not isinstance(n,int):
        print 'defined for int'
        return None
    elif n < 0:
        print 'negative int not allowed'
        return None

def factorial(n):
    if check() == None:
        return -1
    if n == 0:
        return 1
    else:
        return n*factorial(n-1)

def fibonacci(n):
    if check(n) == None:
        return -1

    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fibonacci(n-1) + fibonacci(n-2)

print fibonacci('a')

'''

'''
import string

letters = string.letters + '_'
nums = string.digits

print 'please input a identifier:'
while True:
    myinput = raw_input('identifier to test?')
    if len(myinput) > 1:
        if myinput[0] not in letters:
            print 'invalid first letter'
            continue
        else:
            for otherchar in myinput[1:]:
                if otherchar not in letters + nums:
                    print 'invalid'
                    continue
        print 'ok'
    else:
        print 'num invalid'
        break
'''

def get_value_from_env(k, default):
    v = os.getenv(k)
    if v != None:
        return v
    else:
        return default